
package com.v1.video.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.v1.video.R;
import com.v1.video.domain.AlbumVideoInfo;

import java.util.List;

public class AlbumVideoAdapter extends BaseAdapter {
    List<AlbumVideoInfo> infos;
    Context context;
    private DisplayImageOptions options;

    public AlbumVideoAdapter(Context context,List<AlbumVideoInfo> infos,DisplayImageOptions options) {
        this.infos = infos;
        this.context = context;
        this.options = options;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView != null && convertView instanceof LinearLayout)
            holder = (ViewHolder) convertView.getTag();
        else {
            holder = new ViewHolder();
            convertView = View.inflate(context,R.layout.item_album_video, null);
            holder.image = (ImageView) convertView.findViewById(R.id.iv);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        }
        AlbumVideoInfo info = infos.get(position);
        holder.image.setImageBitmap(null);
        ImageLoader.getInstance().displayImage(info.getImage(), holder.image, options, new SimpleImageLoadingListener(){
           @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                ImageView iv = (ImageView) view;
                iv.setImageBitmap(loadedImage);
            }
        });
        holder.title.setText(info.getTitle());
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public int getCount() {
        return infos.size();
    }
    
    static class ViewHolder {
        TextView title;
        ImageView image;
    }
}
